/*
 * 软件版权: 恒生电子股份有限公司
 * 修改记录:
 * 修改日期     修改人员  修改说明
 * ========    =======  ============================================
 * 2021/4/9  lisy31662  新增
 * ========    =======  ============================================
 */
package com.sansi.pencilbook.controller;

import com.sansi.pencilbook.DO.ResponseDO;
import com.sansi.pencilbook.DO.Result;
import com.sansi.pencilbook.entity.Order;
import com.sansi.pencilbook.service.OrderServer;
import com.sansi.pencilbook.util.ResponseDOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

/**
 * 功能说明: 订单控制器
 *
 * @author lisy31662
 */
@Slf4j
@RestController
@RequestMapping("/api/order")
public class OrderController extends VerifyController implements BaseController{

  @Autowired
  private OrderServer orderServer;

  @PostMapping("/u/addOrder")
  public ResponseDO addOrder(@RequestBody List<Order> orders, HttpServletRequest request) {
    if (checkUser(request)) {
      Calendar calendar = Calendar.getInstance();
      int rand = (int) ((Math.random()+1)*10);
      String serialNo = new SimpleDateFormat("yyyyMMddHHmmss").format(calendar.getTime()) + rand;
      for (Order order : orders) {
        order.setSerialNo(serialNo);
      }
      Result result = orderServer.addOrders(orders);
      return ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg());
    }else {
      return ResponseDOUtil.setResponseDO(false, "请登录后在操作");
    }
  }

}
